﻿using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using WpfApp.Tools.Utils;

namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// UC_StaffAccountManage.xaml 的交互逻辑
    /// </summary>
    public partial class UC_StaffAccountManage : UserControl
    {
        public UC_StaffAccountManage()
        {
            InitializeComponent();
        }
        ///实例化服务层
        BLL.UCStaffAccountManage.UCStaffAccountManageClient myClient = new BLL.UCStaffAccountManage.UCStaffAccountManageClient();
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
            #region 禁用按钮
            btnUpdate.IsEnabled = false;//禁用修改按钮
            btnUpdate.Opacity = 0.5;
            btnDelete.IsEnabled = false;//禁用删除按钮
            btnDelete.Opacity = 0.5;
            #endregion

        }
        /// <summary>
        /// 刷新表格
        /// </summary>
        public void GetData()
        {
           System.Data.DataTable dt = myClient.SelectAllStaffAccount().Tables[0];
            dgAccountManage.ItemsSource = dt.DefaultView;
        }
        /// <summary>
        /// 打开新增窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInert_Click(object sender, RoutedEventArgs e)
        {
            //实例化窗口
            WD_InsertStaffAccountManage myInsert = new WD_InsertStaffAccountManage();
            //打开窗口
            myInsert.ShowDialog();
            //刷新页面
            GetData();
        }
         /// <summary>
         /// 打开修改弹窗
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取选中行
                if ((DataRowView)dgAccountManage.SelectedItem != null)
                {
                    DataRowView dr = (DataRowView)dgAccountManage.SelectedItem;
                    //实例化窗口
                    WD_UpdateStaffAccountManage myUpdate = new WD_UpdateStaffAccountManage(dr);
                    //打开窗口
                    myUpdate.ShowDialog();
                    GetData();
                }
                else
                {
                    MessageBox.Show("请选择要修改的数据");
                }

            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 删除账号信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //获取选中行（当前行）
                if ((DataRowView)dgAccountManage.SelectedItem != null)
                {
                    //获取选中行
                    DataRowView drv = (DataRowView)dgAccountManage.SelectedItem;
                    //获取主键
                    int intID = Convert.ToInt32(drv.Row["operatorID"]);
                    //执行删除
                    int intCount = myClient.DeleteOperator(intID);
                    //判断结果
                    if (intCount > 0)
                    {
                        MessageBox.Show("删除数据成功！");
                        GetData();
                    }
                    else
                    {
                        MessageBox.Show("删除数据失败！");
                    }

                }
                

            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 打开导入窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                WD_ImportStaffAccount myImportStaffAccount = new WD_ImportStaffAccount();
                myImportStaffAccount.ShowDialog();
                //刷新页面
                GetData();
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 导出员工数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            if (dgAccountManage.Items.Count > 0)
            {
                #region 调用dll（利用插件导出）
                if (ExporTotExcel.Export(dgAccountManage, "员工信息") == true)
                {
                    MessageBox.Show("数据导出成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    MessageBox.Show("数据导出失败!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                #endregion
                #region CSV格式的导出
                //DataTable dt = myClient.SelectAllStaffAccount().Tables[0];
                //if (ExportExcel.ExportDataGridSaveAs(true, dgAccountManage, dt) == true)
                //{
                //    MessageBox.Show("数据导出成功!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Information);
                //}
                //else
                //{
                //    MessageBox.Show("数据导出失败!", "系统提示", MessageBoxButton.OK, MessageBoxImage.Error);
                //}
                #endregion
                #region 原始的导出
                //System.Data.DataTable dt = myClient.SelectAllStaffAccount().Tables[0];
                ////（1）创建Excel
                //Microsoft.Office.Interop.Excel.Application excelApp=new Microsoft.Office.Interop.Excel.Application();
                ////（2）创建工作簿（WorkBook：即Excel文件主体本身）
                //Workbook excelWB = excelApp.Workbooks.Add(System.Type.Missing);
                ////（3）创建工作表（即Excel里的子表sheet） 1表示在子表sheet1里进行数据导出
                //Worksheet excelWS = (Worksheet)excelWB.Worksheets[1];
                ////（4）如果数据中存在数字类型 可以让它变文本格式显示
                //excelWS.Cells.NumberFormat = "@";
                ////（5）将数据导入到工作表的单元格
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    for (int j = 0; j < dt.Columns.Count; j++)
                //    {
                //        //Excel单元格第一个从索引1开始
                //        excelWS.Cells[i + 1, j + 1] = dt.Rows[i][j].ToString();
                //    }
                //}
                ////（6）将其进行保存到指定的路径
                //excelWB.SaveAs("E:\\员工账户信息Excel文件.xlsx");
                //excelWB.Close();
                //excelApp.Quit();
                ////KillAllExcel(excelApp); //释放可能还没释放的进程
                #endregion
            }
            else
            {
                MessageBox.Show("表格没有数据");
            }
            

        }
        /// <summary>
        /// 模糊搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Select_SelectionChanged(object sender, RoutedEventArgs e)
        {
            try
            {
                string select = "";
                string strConent = txt_Select.Text.Trim();
                if (strConent != "")
                {
                    //模糊查询内容
                    select += "staffName like '%" + strConent + "%'" +
                            "OR operatorAccounts like '%" + strConent + "%'" +
                            "OR operatorPassword like '%" + strConent + "%'" +
                            "OR permissionGroupName like '%" + strConent + "%'";
                }
                //全部数据
                System.Data.DataTable dtAll = myClient.SelectAllStaffAccount().Tables[0];
                DataView dv = new DataView(dtAll);
                //筛选出来的而数据
                System.Data.DataTable dt = new System.Data.DataTable();
                if (select != "")
                {
                    //筛选数据
                    dv.RowFilter = select;
                    dt = dv.ToTable();
                }
                if (select == "")
                {
                    //查询全部数据
                    dt = dv.ToTable();
                }
                dgAccountManage.ItemsSource = dt.DefaultView;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 选中行（按钮的禁用和启用）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgAccountManage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //获取选中行
                if ((DataRowView)dgAccountManage.SelectedItem != null)
                {
                    btnUpdate.IsEnabled = true;
                    btnUpdate.Opacity = 1;
                    btnDelete.IsEnabled = true;
                    btnDelete.Opacity = 1;
                }
                else
                {
                    btnUpdate.IsEnabled = false;
                    btnUpdate.Opacity = 0.5;
                    btnDelete.IsEnabled = false;
                    btnDelete.Opacity = 0.5;
                }


            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
